package com.ruoyi.erp.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.erp.domain.ErpPs;
import com.ruoyi.erp.service.IErpPsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 计划订单Controller
 * 
 * @author ruoyi
 * @date 2021-12-10
 */
@RestController
@RequestMapping("/erp/ps")
public class ErpPsController extends BaseController
{
    @Autowired
    private IErpPsService erpPsService;

    /**
     * 查询计划订单列表
     */
    @PreAuthorize(hasPermi = "erp:ps:list")
    @GetMapping("/list")
    public TableDataInfo list(ErpPs erpPs)
    {
        startPage();
        List<ErpPs> list = erpPsService.selectErpPsList(erpPs);
        return getDataTable(list);
    }



    /**
     * 导出计划订单列表
     */
    @PreAuthorize(hasPermi = "erp:ps:export")
    @Log(title = "计划订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpPs erpPs) throws IOException
    {
        List<ErpPs> list = erpPsService.selectErpPsList(erpPs);
        ExcelUtil<ErpPs> util = new ExcelUtil<ErpPs>(ErpPs.class);
        util.exportExcel(response, list, "计划订单数据");
    }

    /**
     * 获取计划订单详细信息
     */
    @PreAuthorize(hasPermi = "erp:ps:query")
    @GetMapping(value = "/{erpPsUuid}")
    public AjaxResult getInfo(@PathVariable("erpPsUuid") String erpPsUuid)
    {
        return AjaxResult.success(erpPsService.selectErpPsByErpPsUuid(erpPsUuid));
    }

    /**
     * 新增计划订单
     */
    @PreAuthorize(hasPermi = "erp:ps:add")
    @Log(title = "计划订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpPs erpPs)
    {
        ErpPs e = new ErpPs();
        e.setDocNum(erpPs.getDocNum());
        List<ErpPs> listByDocNum = erpPsService.selectErpPsList(e);
        if(listByDocNum.size()>0){
           return new AjaxResult(1521,"您提交的计划订单号已存在！请重新填写！");
        }
        return toAjax(erpPsService.insertErpPs(erpPs));
    }

    /**
     * 修改计划订单
     */
    @PreAuthorize(hasPermi = "erp:ps:edit")
    @Log(title = "计划订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpPs erpPs)
    {
        return toAjax(erpPsService.updateErpPs(erpPs));
    }

    /**
     * 删除计划订单
     */
    @PreAuthorize(hasPermi = "erp:ps:remove")
    @Log(title = "计划订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{erpPsUuids}")
    public AjaxResult remove(@PathVariable String[] erpPsUuids)
    {
        return toAjax(erpPsService.deleteErpPsByErpPsUuids(erpPsUuids));
    }
}
